package cetcbigdata.da.service.xinjiang.spider2;

import cetcbigdata.da.base.Base;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class Spider2 extends Base {

    //包含清单：
    // 行政权力事项清单、公共服务事项清单、
    // 最多跑一趟事项清单、不见面办理事项清单、
    // 移动办清单、 通办清单、
    // 网上办清单、 预约清单、
    // 就近办清单、 自助清单。
    final String text = "[{'name':'行政权力事项清单','params':{'token':'Epoint_WebSerivce_**##0601','params':{'tasktype':'','pagesize':15,'handletype':'','taskname':''}}}," +
            "{'name':'公共服务事项清单','params':{'token':'Epoint_WebSerivce_**##0601','params':{'deptcode':'','pagesize':15,'tasktype':'20','handletype':'','taskname':''}}}," +
            "{'name':'最多跑一趟事项清单','params':{'token':'Epoint_WebSerivce_**##0601','params':{'tasktype':'','deptcode':'','pagesize':15,'taskname':'','handletype':'','num':'1','general':'general'}}}," +
            "{'name':'不见面办理事项清单','params':{'token':'Epoint_WebSerivce_**##0601','params':{'tasktype':'','deptcode':'','pagesize':15,'taskname':'','handletype':'','num':'0','general':'general'}}}," +
            "{'name':'移动办清单','params':{'token':'Epoint_WebSerivce_**##0601','params':{'deptcode':'','pagesize':15,'tasktype':'','handletype':'','isydb':'1','taskname':''}}}," +
            "{'name':'通办清单','params':{'token':'Epoint_WebSerivce_**##0601','params':{'deptcode':'','pagesize':15,'tasktype':'','handletype':'','isydb':'1','taskname':''}}}," +
            "{'name':'网上办清单','params':{'token':'Epoint_WebSerivce_**##0601','params':{'deptcode':'','pagesize':15,'tasktype':'','handletype':'','iswsb':'1','taskname':''}}}," +
            "{'name':'预约办清单','params':{'token':'Epoint_WebSerivce_**##0601','params':{'deptcode':'','pagesize':15,'tasktype':'','handletype':'','isyyb':'1','taskname':''}}}," +
            "{'name':'就近办清单','params':{'token':'Epoint_WebSerivce_**##0601','params':{'deptcode':'','pagesize':15,'tasktype':'','handletype':'','istb':'1','taskname':''}}}," +
            "{'name':'自助办清单','params':{'token':'Epoint_WebSerivce_**##0601','params':{'deptcode':'','pagesize':15,'tasktype':'','handletype':'','istb':'1','taskname':''}}}]";


    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        return null;
    }

    public JSONObject parseListPage(){
        //地区划分
        String url = "https://zwfw.xinjiang.gov.cn/xqldjkxt/rest/area/getAreaList";
        JSONObject parmas = JSONObject.parseObject("{'params':{'areacode':'650000'}}");
        String strResponse = postReq(url, parmas);

        JSONObject response = JSONObject.parseObject(strResponse);
        JSONObject custom = response.getJSONObject("custom");
        //获取市级地区列表
        JSONArray SJ_arealist = custom.getJSONArray("arealist");
        if (null != SJ_arealist && SJ_arealist.size() > 0){
            for (Object o : SJ_arealist){
                JSONObject SJ_area = (JSONObject) o;

                parmas = JSONObject.parseObject("{'params':{'areacode':"+ SJ_area.getString("areacode") +"}}");
                strResponse = postReq(url, parmas);
                response = JSONObject.parseObject(strResponse);
                custom = response.getJSONObject("custom");

                StringBuilder areaTitle = new StringBuilder();
                for (Object o1 : custom.getJSONArray("areatitlelist")){
                    JSONObject object1 = (JSONObject) o1;
                    areaTitle.append(object1.getString("areaname")).append("-");
                }
                //获取区级地区列表
                JSONArray QJ_arealist = custom.getJSONArray("arealist");
                if (null != QJ_arealist && QJ_arealist.size() > 0) {
                    for (Object o1 : QJ_arealist) {
                        JSONObject QJ_area = (JSONObject) o1;
                        String region = areaTitle + QJ_area.getString("areaname");
                        String areacode = QJ_area.getString("areacode");

                        JSONArray themeList = JSONArray.parseArray(text);
                        String themeUrl = "https://zwfw.xinjiang.gov.cn/xjzwdt/rest/task/getywTask";
                        for (Object o2 : themeList){
                            JSONObject themeReqData = (JSONObject) o2;
                            String module = themeReqData.getString("name");
                            JSONObject themeParams = themeReqData.getJSONObject("params");
                            //当前页数、总页数
                            int pags = 2;
                            int currentPage = 0;
                            //将当前页数封装进请求参数
                            themeParams.getJSONObject("params").put("currentpage", currentPage);
                            //将地区划分
                            themeParams.getJSONObject("params").put("areacode", areacode);
                            while (pags > currentPage) {
                                try {
                                    String themeStrResp = postReq(themeUrl, themeParams);
                                    JSONObject themeResp = JSONObject.parseObject(themeStrResp);
                                    JSONObject custom1 = themeResp.getJSONObject("custom");
                                    JSONArray catalogs = custom1.getJSONArray("catalogs");
                                    for (Object o3 : catalogs){
                                        JSONObject catalog = (JSONObject) o3;
                                        JSONArray tasklist = catalog.getJSONArray("tasklist");
                                        for (Object o4 : tasklist){
                                            JSONObject task = (JSONObject) o4;
                                            String dataUrl;
                                            JSONObject result = new JSONObject();
                                            result.put("名称", task.getString("taskname"));
                                            String taskcode = task.getString("taskcode");
                                            if (null != task.getJSONArray("tasks") && task.getJSONArray("tasks").size() > 0){
                                                dataUrl = "https://zwfw.xinjiang.gov.cn/listDetailsnew.html?taskcode="+taskcode;
                                            }else {
                                                dataUrl = "https://zwfw.xinjiang.gov.cn/bmfwtest/guidetest/guidance.html?taskcode="+taskcode;
                                            }
                                            result.put("地址", dataUrl);
                                            //测试用代码
                                            System.out.println(
                                                    dataUrl + "\n" +
                                                            region + "\n" +
                                                            module + "\n" +
                                                            result + "\n"
                                            );
                                            return processResult(
                                                    null,
                                                    themeStrResp,
                                                    dataUrl,
                                                    "",
                                                    result,
                                                    region,
                                                    module
                                            );
                                        }
                                    }
                                    currentPage++;
                                    pags = (int) Math.ceil((double) custom1.getInteger("totalcount") / 15);
                                } catch (Exception e) {
                                    logger.info("无数据");
                                }
                            }
                        }
                    }
                }
            }
        }
        return null;
    }

    public static void main(String[] args) {
        Spider2 spider2 = new Spider2();
        spider2.parseListPage();
    }
}
